﻿using EmbeddedSensorCloud.Command;
using EmbeddedSensorCloud.Core;
using EmbeddedSensorCloud.Core.Server;
using System;

namespace EmbeddedSensorCloud.Console.Commands
{
    public class ExitCommand : CommandBase
    {
        private readonly ServerCore worker;

        public ExitCommand(ServerCore worker)
        {
            if (worker == null)
            {
                throw new ArgumentException("ServerCore cannot be null");
            }

            this.worker = worker;
        }

        public override string Description
        {
            get { return "Close all resources and exit."; }
        }

        public override string Name
        {
            get { return "exit"; }
        }

        public override string Syntax
        {
            get { return "exit"; }
        }

        protected override void execute(string[] param)
        {
            try
            {
                this.worker.Stop();
            }
            catch (ServerNotListeningException)
            {
            }
            catch (ServerException ex)
            {
                base.RaiseLogged(string.Format("Server stopping failed:{0}   * {1} *", Environment.NewLine, ex));
            }

            Environment.Exit(0);
        }

        public override string RequiredArgumentRegex
        {
            get { return ""; }
        }
    }
}
